package demoMod.invincibleOne.cards.invincible;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.GameActionManager;
import com.megacrit.cardcrawl.actions.animations.VFXAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.DamageAllEnemiesAction;
import com.megacrit.cardcrawl.actions.defect.ChannelAction;
import com.megacrit.cardcrawl.actions.unique.RemoveAllPowersAction;
import com.megacrit.cardcrawl.actions.utility.SFXAction;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.orbs.Dark;
import com.megacrit.cardcrawl.orbs.Frost;
import com.megacrit.cardcrawl.orbs.Lightning;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.powers.FocusPower;
import com.megacrit.cardcrawl.vfx.combat.MindblastEffect;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.relics.TaskGoal;

import java.util.stream.Collectors;

public class HyperBeam extends AbstractInvincibleCard {
    public static final String ID = InvincibleOneMod.makeID("HyperBeam");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/HyperBeam.png";

    private static final CardType TYPE = CardType.ATTACK;
    private static final CardRarity RARITY = CardRarity.BASIC;
    private static final CardTarget TARGET = CardTarget.ALL_ENEMY;

    private static final int COST = 2;

    public HyperBeam() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 2;
        this.baseDamage = 52;
        this.isMultiDamage = true;;
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
            upgradeMagicNumber(2);
            upgradeDamage(16);
        };
    }


    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        addToBot(new SFXAction("ATTACK_HEAVY"));
        addToBot(new VFXAction(p, new MindblastEffect(p.dialogX, p.dialogY, p.flipHorizontal), 0.1F));

        int maxCount = 7;
        maxCount += AbstractDungeon.player.masterDeck.group.stream().filter(c -> c instanceof BrokenWatch && !c.upgraded).count();
        maxCount += 2 * AbstractDungeon.player.masterDeck.group.stream().filter(c -> c instanceof BrokenWatch && c.upgraded).count();
        if (TaskGoal.combatTurn >= maxCount) {
            addToBot(new AbstractGameAction() {
                @Override
                public void update() {
                    (AbstractDungeon.getCurrRoom()).cannotLose = false;
                    for (AbstractMonster m2 : AbstractDungeon.getMonsters().monsters.stream().filter(m2 -> !m2.isDeadOrEscaped() || m2.halfDead).collect(Collectors.toList())) {
                        addToBot(new RemoveAllPowersAction(m2, false));
                        addToBot(new AbstractGameAction() {
                            @Override
                            public void update() {
                                m2.currentHealth = 0;
                                m2.healthBarUpdatedEvent();
                                m2.die();
                                isDone = true;
                            }
                        });
                    }
                    addToBot(new AbstractGameAction() {
                        @Override
                        public void update() {
                            AbstractDungeon.getCurrRoom().endBattle();
                        }
                    });
                    isDone = true;
                }
            });
        } else {
            addToBot(new ApplyPowerAction(p, p, new FocusPower(p, magicNumber), magicNumber));
            addToBot(new DamageAllEnemiesAction(p, this.multiDamage, this.damageTypeForTurn, AbstractGameAction.AttackEffect.NONE));
        }
    }
}
